<?php

namespace App\Models;

use DateTimeInterface;

class OrderModel extends RbhBaseModel
{
    const ORDER_NOT_PAYED = '未支付';
    const ORDER_PAYED = '待发货';
    const ORDER_EXPRESSED = '已发货';
    const ORDER_REFUNDING = '退货中';
    const ORDER_REFUNDED = '退单';
    const ORDER_FINISHED = '交易完成';
    const ORDER_REFUND_EXPRESSED = '已寄回';

    protected $table = 'df_order';

    protected $guarded = [];

    /**
     * 获取模型
     * @param $request
     * @return mixed
     */
    public static function getModel($request): mixed
    {
        $Model = self::where('id', '>', 0);
        return self::getQueryModel($Model, $request);
    }

    /**
     * 获取查询模型
     * @param $Model
     * @param $request
     * @return mixed
     */
    public static function getQueryModel($Model, $request): mixed
    {
        if ($request->input('pay_status') !== null)
            $Model = $Model->where('pay_status', $request->input('pay_status', 0));
        if ($request->input('refund_status') !== null)
            $Model = $Model->where('refund_status', $request->input('refund_status', 0));
        if ($request->input('express_status') !== null)
            $Model = $Model->where('express_status', $request->input('express_status', 0));
        if ($request->input('done_status') !== null)
            $Model = $Model->where('done_status', $request->input('done_status', 0));
        if ($request->input('finish_status') !== null)
            $Model = $Model->where('finish_status', $request->input('finish_status', 0));
        if ($request->input('uid'))
            $Model = $Model->where('uid', $request->input('uid'));
        if ($request->input('username'))
            $Model = $Model->where('username', $request->input('username'));
        if ($request->input('building'))
            $Model = $Model->where('building', $request->input('building'));
        if ($request->input('floor'))
            $Model = $Model->where('floor', $request->input('floor'));
        if ($request->input('sname'))
            $Model = $Model->where('sname', $request->input('sname'));
        if ($request->input('start'))
            $Model = $Model->whereDate('created_at', '>=', $request->input('start'));
        if ($request->input('end'))
            $Model = $Model->whereDate('created_at', '<=', $request->input('end'));
        if ($request->input('remark'))
            $Model = $Model->where('remark', 'like', '%'. $request->input('remark') .'%');
        if ($request->input('keyword')) {
            $keyword = $request->input('keyword');
            $Model = $Model->where(function ($query) use ($keyword) {
                $query->where('order_no', $keyword)->whereOr('express_no', $keyword)->whereOr('mobile', $keyword);
            });
        }
        return $Model;
    }

    /**
     * 创建订单
     * @param $uid
     * @param $orderNo
     * @param $expressFee
     * @param $postData
     * @return mixed
     */
    public static function createOrder($uid, $orderNo, $expressFee, $postData): mixed
    {
        return self::create(array_merge([
            'uid'             => $uid,
            'order_no'        => $orderNo,
            'express_fee'     => $expressFee
        ], $postData));
    }

    /**
     * 订单状态码
     * @param $order
     * @return int
     */
    public static function getOrderStatusCode($order): int
    {
        $binaryStatus = $order->finish_stauts . $order->refund_status . $order->done_status
            . $order->express_status . $order->pay_status;
        $decimalStatus = bindec($binaryStatus);
        return match (true) {
            $decimalStatus <= 1, $decimalStatus == 3 => $decimalStatus,
            $decimalStatus >= 17 => 23,
            $decimalStatus >= 9 => 9,
            default => 1
        };
    }

    /**
     * 订单状态描述
     */
    public static function getOrderStatusText($order): string
    {
        $binaryStatus = $order->finish_stauts . $order->refund_status . $order->done_status
            . $order->express_status . $order->pay_status;
        $decimalStatus = bindec($binaryStatus);
        return match (true) {
            $decimalStatus == 0 => self::ORDER_NOT_PAYED,
            $decimalStatus == 1 => self::ORDER_PAYED,
            $decimalStatus == 3 => self::ORDER_EXPRESSED,
            $decimalStatus >= 25 => self::ORDER_REFUNDED,
            $decimalStatus >= 17 => self::ORDER_FINISHED,
            $decimalStatus >= 9  => self::ORDER_REFUNDING,
        };
    }

    /**
     * @param DateTimeInterface $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date): string
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }
}
